package sv.edu.edutec.fotos.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import sv.edu.edutec.fotos.model.TblComment;
import sv.edu.edutec.fotos.model.TblPhoto;
import sv.edu.edutec.fotos.model.TblUser;

@Repository
public class PhotoDAOImpl implements PhotoDAO {
	
	@Autowired
	SessionFactory sessionFactory;
	
	public void addPhoto(TblPhoto photo) {
		// TODO Auto-generated method stub
		this.sessionFactory.getCurrentSession().save(photo);
	}

	public void editPhoto(TblPhoto photo) {
		// TODO Auto-generated method stub
		this.sessionFactory.getCurrentSession().update(photo);
	}

	public void deletePhoto(Integer photoId) {
		// TODO Auto-generated method stub
		TblPhoto photo = (TblPhoto) this.sessionFactory.getCurrentSession().load(TblPhoto.class,photoId);
		if(photo != null){
			this.sessionFactory.getCurrentSession().delete(photo);
		}
	}

	public List<TblPhoto> listPhotos() {
		// TODO Auto-generated method stub
		String hql = "from TblPhoto";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	@Override
	public TblPhoto findPhoto(Integer photoId) {
		// TODO Auto-generated method stub
		return (TblPhoto) this.sessionFactory.getCurrentSession().get(TblPhoto.class, photoId);
	}

	@Override
	public void addComment(TblComment comment) {
		// TODO Auto-generated method stub
		this.sessionFactory.getCurrentSession().save(comment);
	}

	@Override
	public List<TblComment> listComments() {
		// TODO Auto-generated method stub
		String hql = "from TblComment";
		return this.sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	@Override
	public List<TblComment> listComments(String hql) {
		// TODO Auto-generated method stub
		return this.sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	@Override
	public List<TblUser> listUsers(String hql) {
		// TODO Auto-generated method stub
		return this.sessionFactory.getCurrentSession().createQuery(hql).list();
	}

}
